package com.eternal.notification.view;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.eternal.notification.R;

/**
 * Created by Administrator
 * On 2020/5/9
 * Description:
 * @author Administrator
 */
public class TitleSelectView extends ConstraintLayout {
    private TextView title;
    private TextView info;
    private ImageView select;
    private boolean noDouble;
    private OnSelectListener listener;

    public TitleSelectView(Context context) {
        this(context, null);
    }

    public TitleSelectView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleSelectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.TitleSelectView);
        title.setText(array.getString(R.styleable.TitleSelectView_title));
        info.setText(array.getString(R.styleable.TitleSelectView_info));
        select.setSelected(array.getBoolean(R.styleable.TitleSelectView_select, false));
        noDouble=array.getBoolean(R.styleable.TitleSelectView_noDouble, false);
        array.recycle();
    }

    private void initView(Context context){
        View view=View.inflate(context, R.layout.layout_title_select, this);
        title=view.findViewById(R.id.tv_mode_type);
        info=view.findViewById(R.id.tv_mode_type_des);
        select=view.findViewById(R.id.iv_select);
        select.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(noDouble){
                    if(!getSelect()){
                        setSelect(true);
                        if(listener!=null){
                            listener.onchange(true);
                        }
                    }
                }else{
                    boolean b=!getSelect();
                    setSelect(b);
                    if(listener!=null){
                        listener.onchange(b);
                    }
                }
            }
        });
    }

    public void setTitle(String title){
        this.title.setText(title);
    }

    public void setInfo(String info){
        this.info.setText(info);
    }

    public void setSelect(boolean select){
        this.select.setSelected(select);
    }

    public boolean getSelect(){
        return select.isSelected();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_UP&&listener!=null){
            boolean b=!getSelect();
            setSelect(b);
            if(listener!=null){
                listener.onchange(b);
            }
        }
        return true;
    }

    public void setSelectListener(OnSelectListener listener){
        this.listener=listener;
    }

    public interface OnSelectListener{
        void onchange(boolean b);
    }
}
